<template>
    <div class="right">
        <div class="header">会员中心-我的圈子</div>
        <div class="shanghcuan" @click="fabu">快速发布</div>
        <div class="content">
            <div class="tabs">
                <div v-for="(item, index) in navList" :key="index" @click="navClick(item)"
                    :class="navIndex == item.label ? 'tabs_items_active' : 'tabs_items'">
                    {{ item.label }}
                </div>
            </div>
            <div class="contents" v-loading="loading">
                <div class="items" v-for="(item, index) in list" :key="index">
                    <image-preview :src="item.avatarImage" :width="50" :height="50" />
                    <div class="right">
                        <div class="title">{{ item.content }}</div>
                        <div class="bottom">
                            <div class="hits">{{ item.hits }}</div>
                            <div class="createTime">{{ item.createTime }}</div>
                        </div>
                    </div>
                </div>
                <div class="zanwu" v-if="total == 0">暂无数据</div>
            </div>
            <div class="paginations">
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" :layout="'prev, pager, next'"
                    :prevText="'上一页'" :nextText="'下一页'" />
            </div>
        </div>

        <el-dialog title="发布圈子" v-model="open" width="600px" append-to-body>
            <el-form ref="formDataRef" :model="formData" :rules="rules" label-width="60px" scroll-to-error>

                <el-row>
                    <el-col :span="24">
                        <el-form-item label="内容" prop="content">
                            <el-input clearable placeholder="请输入内容" v-model="formData.content" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="24">
                        <el-form-item label="类别" prop="type">
                            <el-select v-model="formData.type" clearable filterable>
                                <el-option v-for="item in djj_popularize_type" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="24">
                        <el-form-item label="图片" prop="image">
                            <ImageUploadDrags v-model="formData.image" :limit="9" :format="true" />
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <div class="upload_button" @click="submitForm(formDataRef)">快速发布</div>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import { getInfo } from '@/api/index'
import { circlemyList, getCircleByUserId, addCircle } from '@/api/index'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const djj_popularize_type = ref([])
const list = ref([])
const navList = ref([
])
const navIndex = ref('全部')
const queryParams = ref({
    pageNum: 1, pageSize: 10, type: ''
})
const loading = ref(false)
const total = ref(0)


const open = ref(false)
const formDataRef = ref()
const formData = ref({
    content: '',
    type: '',
    image: '',
})
const rules = ref({
    content: [
        { required: true, message: '请输入内容', trigger: 'blur' },
    ],
    type: [
        { required: true, message: '请选择类别', trigger: 'change' },
    ],
    image: [
        { required: true, message: '请上传图片', trigger: 'change' },
    ],
})
// 圈子
const getCircleList = async () => {
    const res = await getInfo()
    const circleCategory =  res.data.user.circleCategory.split(',')
    djj_popularize_type.value = circleCategory.map(item => {
        return {
            label: item,
            value: item
        }
    })
}


const navClick = (item) => {
    navIndex.value = item.label
    queryParams.value.type = item.value

    queryParams.value.pageNum = 1
    getList()
}
const getNavlist = async () => {
    const res = await getCircleByUserId({})
    const navLists = []
    navLists.push({
        label: '全部',
        value: '',
    },)
    res.msg.split(',').forEach((element, index) => {
        navLists.push({
            label: element,
            value: element
        })
    });
    navList.value = navLists
}
const getList = async () => {
    loading.value = true
    const res = await circlemyList(queryParams.value)
    res.rows.forEach(element => {
        element.createTime = element.createTime.replace(/-/g, "/")
        element.avatarImage = '/' + element.avatarImage
    });
    total.value = res.total
    list.value = res.rows
    loading.value = false
}
const fabu = () => {
    open.value = true
}
/** 取消 */
const cancel = () => {
    open.value = false
    formData.value = {
        content: '',
        type: '',
        image: '',
    }
}
/** 确定 */
const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate(async (valid: any, fields: any) => {
        if (valid) {
            await addCircle(formData.value)
            proxy?.$modal.msgSuccess('发布圈子成功')
            cancel()
            formEl.resetFields();
            getList()
        }
    })
}
onMounted(async () => {
    getNavlist()
    getList()
    getCircleList()
});
</script>
<style scoped lang="scss">
.dialog-footer {
    display: flex;
    justify-content: center;

    .upload_button {
        width: 150px;
        height: 40px;
        background: #22AFE5;
        border-radius: 14px;
        text-align: center;
        line-height: 40px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        color: #FFFFFF;
        cursor: pointer;
    }
}

.right {
    width: calc(100% - 410px);
    height: 100%;
    position: relative;

    .header {
        width: 100%;
        padding: 15px 0;
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 18px;
        color: #000000;
        border-bottom: 1px solid #E6E6E6;
    }

    .shanghcuan {
        position: absolute;
        top: 15px;
        right: 20px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 18px;
        color: #000000;
        cursor: pointer;
    }

    .content {
        width: 100%;
        height: calc(100% - 55px);
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-content: flex-start;
        box-sizing: border-box;
        padding: 40px 0 0 0;

        .tabs {
            width: 100%;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #E6E6E6;
            line-height: 2;
            overflow: auto;

            .tabs_items {
                margin-right: 30px;
                color: #A0A0A0;
                font-size: 16px;
                cursor: pointer;
            }

            .tabs_items_active {
                margin-right: 30px;
                color: #000000;
                font-size: 18px;
                cursor: pointer;
            }
        }

        .contents {
            width: 100%;
            height: calc(100% - 109px);
            display: flex;
            justify-content: flex-end;
            align-items: flex-start;
            flex-wrap: wrap;
            align-content: flex-start;
            overflow-y: auto;

            .zanwu {
                width: 100%;
                text-align: center;
                color: #A0A0A0;
                font-size: 16px;
                margin-top: 50%;
                transform: translateY(-50%);
            }

            .items {
                width: 100%;
                height: 55px;
                border-bottom: 1px solid #E6E6E6;
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 20px 0;
                box-sizing: content-box;
                padding-left: 4px;

                img {
                    width: 65px;
                    height: 51px;
                    border-radius: 5px;
                }

                .right {
                    width: calc(100% - 100px);
                    height: 51px;
                    display: flex;
                    flex-wrap: wrap;

                    .title {
                        width: 100%;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        font-size: 14px;
                        color: #000000;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                    }

                    .bottom {
                        width: 100%;
                        display: flex;
                        align-items: center;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        font-size: 12px;
                        color: #969696;

                        .hits {
                            margin-right: 20px;
                        }
                    }
                }
            }
        }

        .paginations {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

    }
}
</style>